package com.demo.controller;

import com.demo.pojo.Dept;
import com.demo.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenyuan
 * @date 2020/9/28 18:04
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     *  正常执行的方法
     * @param id
     * @return
     */
    @GetMapping("/queryDeptById/{id}")
    @HystrixCommand(fallbackMethod = "hystrixQueryDeptById")
    public Dept queryDeptById(@PathVariable("id") Long id){
        Dept dept = deptService.queryDeptById(id);

        //处理异常
        if (dept == null) {
            throw new RuntimeException("id =>【" + id + "】 无法获取改用户，或不存在！");
        }
        return dept;
    }

    /**
     * 上面方法出现异常后执行的备选方案
     * @return
     */
    public Dept hystrixQueryDeptById(@PathVariable("id") Long id){
        return new Dept(id,"【Hystrix】id =>【" + id + "】 无法获取改用户，或不存在！");
    }


    @GetMapping("/queryDepts")
    public List<Dept> queryDepts() {
        return deptService.queryDepts();
    }


}
